/**
 * 
 */
package com.ccqlk.attendance.config;

import java.util.List;

import org.beetl.core.misc.BeetlUtil;
import org.beetl.ext.spring.BeetlGroupUtilConfiguration;
import org.beetl.ext.spring.BeetlSpringViewResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.http.MediaType;
import org.springframework.web.accept.ContentNegotiationManager;
import org.springframework.web.accept.ContentNegotiationManagerFactoryBean;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.ContentNegotiatingViewResolver;

import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter4;
import com.alibaba.fastjson.support.spring.FastJsonJsonView;
import com.ccqlk.attendance.common.converter.CustomDateConverter;
import com.ccqlk.attendance.common.converter.CustomTimeConverter;
import com.google.common.collect.Lists;

/**
 * Copyright 2015-2018 重庆鲁控科技有限公司<br>
 * 文件标题: SpringMvcConfig<br>
 * 文件说明: TODO<br>
 * @author ZhangYinxian 348893009@qq.com<br>
 * 创建时间 2016年5月26日 下午10:45:53<br>
 */
@Configuration
@EnableWebMvc
@ComponentScan(value = {"com.ccqlk.attendance.controller","com.ccqlk.attendance.api.controller"})
public class SpringMvcConfig extends WebMvcConfigurerAdapter {

	@Bean
	public FastJsonHttpMessageConverter4 fastJsonHttpMessageConverter4(){
		FastJsonHttpMessageConverter4 fastJsonHttpMessageConverter4 = new FastJsonHttpMessageConverter4();
		List<MediaType> supportedMediaTypes = Lists.newArrayList();
		supportedMediaTypes.add(MediaType.TEXT_HTML);
		supportedMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
		
		fastJsonHttpMessageConverter4.setSupportedMediaTypes(supportedMediaTypes);
		return fastJsonHttpMessageConverter4;
	}
	
	@Bean
	public ContentNegotiationManager contentNegotiationManager(){
		ContentNegotiationManagerFactoryBean contentNegotiationManagerFactoryBean = new ContentNegotiationManagerFactoryBean();
		
		contentNegotiationManagerFactoryBean.setFavorParameter(true);
		contentNegotiationManagerFactoryBean.setFavorPathExtension(true);
		contentNegotiationManagerFactoryBean.setParameterName("format");
		contentNegotiationManagerFactoryBean.addMediaType("json", MediaType.APPLICATION_JSON);
		contentNegotiationManagerFactoryBean.addMediaType("html", MediaType.TEXT_HTML);
		contentNegotiationManagerFactoryBean.addMediaType("xml", MediaType.APPLICATION_XML);
		
		contentNegotiationManagerFactoryBean.setDefaultContentType(MediaType.TEXT_HTML);
		
		return contentNegotiationManagerFactoryBean.getObject();
	}
	
	@Bean
	public ContentNegotiatingViewResolver contentNegotiatingViewResolver(){
		ContentNegotiatingViewResolver contentNegotiatingViewResolver = new ContentNegotiatingViewResolver();
		
		contentNegotiatingViewResolver.setDefaultViews(Lists.newArrayList(new FastJsonJsonView()));
		contentNegotiatingViewResolver.setContentNegotiationManager(contentNegotiationManager());
		
		return contentNegotiatingViewResolver;
	}
	
	@Bean(name="viewResolver")
	public BeetlSpringViewResolver beetlSpringViewResolver(){
		BeetlSpringViewResolver beetlSpringViewResolver = new BeetlSpringViewResolver();
		
        BeetlGroupUtilConfiguration beetlGroupUtilConfiguration = new BeetlGroupUtilConfiguration();
		
		beetlGroupUtilConfiguration.setRoot(BeetlUtil.getWebRoot()); //必须配置这个属性，不然会引起beetl无法识别
		
		beetlGroupUtilConfiguration.init();
		beetlSpringViewResolver.setConfig(beetlGroupUtilConfiguration);
		beetlSpringViewResolver.setContentType("text/html;charset=utf-8");
		beetlSpringViewResolver.setPrefix("/WEB-INF/tpl/");
		beetlSpringViewResolver.setSuffix(".html");
		return beetlSpringViewResolver;
	}
	
	/* (non-Javadoc)
	 * @see org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter#addFormatters(org.springframework.format.FormatterRegistry)
	 */
	@Override
	public void addFormatters(FormatterRegistry registry) {
		registry.addConverter(new CustomDateConverter());
		registry.addConverter(new CustomTimeConverter());
		super.addFormatters(registry);
	}
}
